/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import dao.UserDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.UserModel;

/**
 *
 * @author ThanhToan
 */
public class UserController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
        } finally { 
            out.close();
        }
    } 

    private void mapParameterToModel(HttpServletRequest req
			,HttpServletResponse res
				,UserModel userModel) {
        if (req.getParameter("txtUsername") != null) {
			userModel.setUsername(req.getParameter("txtUsername").trim()); }
        if (req.getParameter("txtPassword") != null) {
			userModel.setPassword(req.getParameter("txtPassword").trim()); }
        if (req.getParameter("txtFirstname") != null) {
			userModel.setFirstname(req.getParameter("txtFirstname").trim()); }
        if (req.getParameter("txtLastname") != null) {
			userModel.setLastname(req.getParameter("txtLastname").trim()); }
        if (req.getParameter("txtEmail") != null) {
			userModel.setEmail(req.getParameter("txtEmail").trim()); }
        if (req.getParameter("txtPhone") != null) {
			userModel.setPhone(req.getParameter("txtPhone").trim()); }
        if (req.getParameter("txtAddress") != null) {
			userModel.setAddress(req.getParameter("txtAddress").trim()); }
        userModel.setDay(req.getParameter("cboDay").trim());
        userModel.setMonth(req.getParameter("cboMonth").trim());
        userModel.setYear(req.getParameter("cboYear").trim());
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doPost(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        register(request, response);
    }

    private void register(HttpServletRequest request
			,HttpServletResponse response)throws ServletException, IOException {
        UserModel userModel = new UserModel();
        mapParameterToModel(request, response, userModel);
        if(UserDAO.insertUser(userModel)){
            //success
            response.sendRedirect("//index.jsp");
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
